import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;

public class PrizePool {
    private final List<Prize> prizes;
    private final Lock lock = new ReentrantLock();
    private final Prize participationPrize;
    private int participationCount = 0;

    private final List<Double> probabilities; // 新增概率列表
    private final Random random = new Random();

    public PrizePool() {
        this.prizes = new ArrayList<>();
        this.probabilities = new ArrayList<>();
        prizes.add(new Prize("一等奖", "钻石剑 - 攻击力+10，耐久度1500", 1));
        probabilities.add(0.10); // 10% 的概率
        prizes.add(new Prize("二等奖", "铁剑 - 攻击力+6，耐久度500", 3));
        probabilities.add(0.25); // 25% 的概率
        prizes.add(new Prize("三等奖", "石剑 - 攻击力+4，耐久度200", 7));
        probabilities.add(0.50); // 50% 的概率
        this.participationPrize = new Prize("参与奖", "木棍 - 攻击力+1，耐久度50", Integer.MAX_VALUE);
    }

    public Prize drawPrize(String participantName) {
        lock.lock();
        try {
            double rand = random.nextDouble();
            double cumulativeProbability = 0;
            for (int i = 0; i < prizes.size(); i++) {
                cumulativeProbability += probabilities.get(i);
                if (rand < cumulativeProbability && prizes.get(i).award()) {
                    return prizes.get(i);
                }
            }

            participationPrize.award();
            participationCount++;
            return participationPrize;
        } finally {
            lock.unlock();
        }
    }

    public List<Prize> getPrizes() {
        return prizes;
    }

    public int getParticipationCount() {
        return participationCount;
    }

    public void reset() {
        lock.lock();
        try {
            for (Prize prize : prizes) {
                prize.reset();
            }
            participationCount = 0;
        } finally {
            lock.unlock();
        }
    }
}